package  
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import sourceproxy.ResourceProxy;
	/**
	 * ...
	 * @author ...
	 */
	public class MMScrollBar extends MMInteractive 
	{
		static public const HORIZONTAL:String = "horizontal";
		static public const VERTICAL:String = "vertical";
		private const MIN_HEIGHT:Number = 100;
		
		
		private var _upButton:MMButton;
		private var _downButton:MMButton;
		private var _scrollButton:MMButton;
		
		private var _minPosition:int=0;
		private var _maxPosition:int=0;
		
		private var _direction:String;
		private var _position:int = 0;
		private var _lastPosition:int = -1;
		
		private var _width:Number=19;
		private var _height:Number=MIN_HEIGHT;
		
		private var _needUpdate:Boolean = true;
		private var _scrollRange:Rectangle;
		
		private var _mouseMoveEnable:Boolean = false;
		
		public var onChange:Function;
		
		private var _upButtonUrl:String;
		private var _downButtonUrl:String;
		private var _moveButtonUrl:String;
		
		
		public function MMScrollBar(upButtonUrl:String="/ui/common/scroll_up.png",downButtonUrl:String="/ui/common/scroll_down.png",moveButtonUrl:String="/ui/common/scroll_bar.png") 
		{
			_needUpdate = true;
			mouseChildren = true;
			_upButtonUrl = upButtonUrl;
			_downButtonUrl = downButtonUrl;
			_moveButtonUrl = moveButtonUrl;
		}
		
		override public function onInit():void
		{
			super.onInit();
			_upButton = new MMButton();
			_upButton.backgroupImageUrl = _upButtonUrl;
			_upButton.show();
			addChild(_upButton);
			
			_upButton.onClick = function():void
			{
				--position;
				
			}
			
			_downButton = new MMButton();
			_downButton.backgroupImageUrl = _downButtonUrl;
			_downButton.show();
			_downButton.onClick = function():void
			{
				++position;
				
			}
			addChild(_downButton);
			
			_scrollButton = new MMButton();
			_scrollButton.backgroupImageUrl = _moveButtonUrl;
			_scrollButton.show();
			_scrollButton.onMouseDown = mouseDownHandler;
			
			addChild(_scrollButton);
			
			onMouseDown = stageMoveHandler;
			
			_scrollRange = new Rectangle();
			
			
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			_mouseMoveEnable = true;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, this.stageMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, stageUpHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, stageUpHandler,true);
			
		}
		
		private function stageUpHandler(e:MouseEvent):void 
		{
			if (stage.hasEventListener(MouseEvent.MOUSE_UP))
				stage.removeEventListener(MouseEvent.MOUSE_UP, stageUpHandler);
			if (stage.hasEventListener(MouseEvent.MOUSE_UP))
				stage.removeEventListener(MouseEvent.MOUSE_UP, stageUpHandler, true);
			if(stage.hasEventListener(MouseEvent.MOUSE_MOVE))
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMoveHandler);
			
			
		}
		
		private function stageMoveHandler(e:MouseEvent):void 
		{
			//if (!_mouseMoveEnable) return;
			var point:Point = new Point(this.mouseX, this.mouseY);
			
			//var localPoint:Point = this.globalToLocal(point);
			//if (point.y > (_scrollButton.y + _scrollButton.height))
				//position=Math.ceil(maxPosition * ((point.y - _scrollRange.top) / _scrollRange.height));
			
			position=maxPosition * ((point.y - _scrollRange.top) / _scrollRange.height);
			
		}
		
		override public function onHide():void
		{
			super.onHide();
		}
		
		override public function onShow():void
		{
			super.onShow();
			
			update();
		}
		
		override public function updateData():void
		{
			super.updateData();
			if (_needUpdate)
			{
				_needUpdate = false;
				drawScrollBarBg();
				_upButton.place((width - _upButton.width) / 2, 0);
				if (_maxPosition <= _minPosition)
				{
					enable = false;
				}
				
				var y:Number;
				if (_position== 0)
					y = _scrollRange.top ;
				else
					y = _scrollRange.top  +(_scrollRange.height -_scrollButton.height) * (position / maxPosition);
				_scrollButton.place((width-_scrollButton.width)/2, y );
				_downButton.place((width-_downButton.width)/2, height - _downButton.height);
			}
		}
		
		private function updateScrollBar():void
		{
			_position = Math.min(_position, _maxPosition);
			_position = Math.max(_position, _minPosition);
			if (_lastPosition != _position)
			{
				_lastPosition = _position;
				_scrollButton.place(_scrollButton.x, _scrollRange.top  + (_scrollRange.height-_scrollButton.height ) * (position / maxPosition) );
				onChange && onChange.call(this, position);
			}
			
		}
		
		private function drawScrollBarBg():void
		{
			_scrollRange.left = 0;
			_scrollRange.top = _upButton.height;
			_scrollRange.width = _width
			_scrollRange.height = (height - _upButton.height -_downButton.height);
			
			graphics.clear();
			graphics.beginFill(0x111111);
			graphics.drawRect(_scrollRange.left, _scrollRange.top, _width, _scrollRange.height);
			graphics.endFill();
			
		}
		
		override public function set enable(value:Boolean):void
		{
			if (enable != value)
			{
				super.enable = value;
				_upButton.enable = value;
				_downButton.enable = value;
				_scrollButton.enable = value;
				_needUpdate = true;
			}
			
		}
		
		
		override public function regResource():void
		{
			ResourceProxy.instance().push(_upButtonUrl);
			ResourceProxy.instance().push(_downButtonUrl);
			ResourceProxy.instance().push(_moveButtonUrl);
		}
		
		override public function get width():Number { return _width; }
		
		override public function set width(value:Number):void 
		{
			_width = value;
		}
		
		override public function get height():Number { return _height; }
		
		override public function set height(value:Number):void 
		{
			if (value > MIN_HEIGHT)
			{
				_height = value;
				_needUpdate = true;
				update()
			}
		}
		
		public function get position():int { return _position; }
		
		public function set position(value:int):void 
		{
			_position = value;
			updateScrollBar();
		}
		
		public function get direction():String { return _direction; }
		
		public function set direction(value:String):void 
		{
			_direction = value;
		}
		
		public function get maxPosition():int { return _maxPosition; }
		
		public function set maxPosition(value:int):void 
		{
			if (value <= _minPosition)
			{
				enable = false;
				return;
			}
			else
				enable = true;
			_maxPosition = value;
			_position = 0;
		}
	}

}